package com.youuvs.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.youuvs.domain.MovieTicket;
import com.youuvs.domain.User;
import com.youuvs.domain.UserTicket;
import com.youuvs.dto.BuyTicketDto;
import com.youuvs.dto.GetUserByMovieIdDto;
import com.youuvs.enums.GlobalEnumCode;
import com.youuvs.exception.GlobalCustomException;
import com.youuvs.feign.MovieFeign;
import com.youuvs.feign.UserTicketFeign;
import com.youuvs.mapper.UserMapper;
import com.youuvs.result.JSONResult;
import com.youuvs.service.IUserService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.youuvs.util.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhaoby
 * @since 2022-08-29
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    //@Resource
    @Autowired
    private MovieFeign movieFeign;     //电影票
    //@Resource
    @Autowired
    private UserTicketFeign userTicketFeign;    //用户,票关联
    @Autowired
    private UserMapper userMapper;

    //根据用户id查电影票信息
    @Override
    public void getTicketByUserId(Long id) {
        movieFeign.getTicketByUserId(id);
    }


    //用户买票
    @Override
    public void buyTicket(BuyTicketDto dto) {
        Long movieId = dto.getMovieId();
        String username = dto.getUsername();
        //拿到电影票信息
        JSONResult jsonResult = movieFeign.get(movieId);
        MovieTicket ticket = (MovieTicket)jsonResult.getData();
        //修改余额
        User user = getByUsername(username);
        BigDecimal blance = user.getBalance().subtract(ticket.getPrice());
        BigDecimal zero = new BigDecimal(0);
        int i = blance.compareTo(zero);
        if(i<0){
            throw new GlobalCustomException(GlobalEnumCode.USER_BALANCE_ERROR);
        }
        user.setBalance(blance);
        userMapper.updateById(user);
        //新增用户与电影票数据
        UserTicket userTicket = new UserTicket();
        userTicket.setUserId(user.getId());
        userTicket.setMoveTicketId(ticket.getId());
        userTicket.setStatus(1);
        userTicketFeign.saveOrUpdate(userTicket);


    }

    /**
     * 根据电影Id，查询用户是否拥有此电影票，电影票是否已经被使用，电影票是否过期，过期提示不可观看
     */
    @Override
    public void getTicketByMovieId(GetUserByMovieIdDto dto) {
        Long userId = dto.getUserId();
        Long movieId = dto.getMovieId();
        JSONResult jsonResult = userTicketFeign.get(userId);
        UserTicket userTicket = (UserTicket)jsonResult.getData();
        //不拥有
        AssertUtil.isNotNull(userTicket,GlobalEnumCode.USER_TICKET_ERROR);
        //被使用
        AssertUtil.isTrue(userTicket.getStatus()>0,GlobalEnumCode.USER_USED_ERROR);
        JSONResult jsonResult1 = movieFeign.get(movieId);
        MovieTicket movieTicket = (MovieTicket)jsonResult1.getData();
        //过期
        Date date = new Date();
        Long overdue=date.getTime()-movieTicket.getEndTime().getTime();
        AssertUtil.isTrue(overdue<0,GlobalEnumCode.USER_OVERDUE_ERROR);
    }


    /**
     * 注册送票
     * @param user
     */
    @Override
    public void insAndPresent(User user) {
        //注册
        userMapper.insert(user);
        //随机送票
        JSONResult list = movieFeign.list();
        JSONResult jsonResult = movieFeign.randomTicket();
        Long moveTicketId = (Long)jsonResult.getData();
        UserTicket userTicket = new UserTicket();
        userTicket.setStatus(1);
        userTicket.setUserId(user.getId());
        userTicket.setMoveTicketId(moveTicketId);
        userTicketFeign.saveOrUpdate(userTicket);
    }


    private User getByUsername(String username) {
        // Wrapper条件构造器
        Wrapper<User> wrapper = new EntityWrapper<>();
        wrapper.eq("username", username);
        // 根据条件构造器中的条件，帮助我们查询对应表并设置条件，查询回一条数据
        return selectOne(wrapper);
    }
}


